#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    string delFrontZero(string& num)
    {
        for (int i = 0; i < num.size(); i++)
        {
            if (num[i] != '0')
                return num.substr(i);
        }
        return "0";
    }
    string removeKdigits(string num, int k) {
        int i = 0, n = num.size();
        stack<char> stk;
        for (auto& c : num)
        {
            while (k && stk.size() && c < stk.top())
            {
                stk.pop();
                k--;
            }
            stk.push(c);
        }
        string res;
        while (stk.size())
        {
            if (k <= 0)
                res += stk.top();
            else
                --k;
            stk.pop();
        }
        reverse(res.begin(), res.end());
        res = delFrontZero(res);
        return res;
        return num;
    }
};